package edu.neu.agv_backend.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.neu.agv_backend.dto.AgvFlawDto;
import edu.neu.agv_backend.dto.AjaxResult;
import edu.neu.agv_backend.dto.TableDataInfo;
import edu.neu.agv_backend.entity.AgvFlaw;
import edu.neu.agv_backend.mapper.AgvFlawMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FlawFromDBService {

    private final AgvFlawMapper flawMapper;

    public FlawFromDBService(AgvFlawMapper flawMapper) {
        this.flawMapper = flawMapper;
    }

    /**
     * 分页查询缺陷列表
     */
    public TableDataInfo<AgvFlawDto> listFlaw(int pageNum, int pageSize) {
        Page<AgvFlaw> page = new Page<>(pageNum, pageSize);
        flawMapper.selectPage(page, null);
        List<AgvFlawDto> rows = page.getRecords().stream().map(this::toDto).collect(Collectors.toList());

        TableDataInfo<AgvFlawDto> info = new TableDataInfo<>();
        info.setTotal(page.getTotal());
        info.setRows(rows);
        info.setCode(200);
        info.setMsg("success");
        return info;
    }

    public AjaxResult<AgvFlawDto> getFlaw(Long id) {
        AgvFlaw flaw = flawMapper.selectById(id);
        AjaxResult<AgvFlawDto> result = new AjaxResult<>();
        if (flaw != null) {
            result.setCode(200);
            result.setMsg("success");
            result.setData(toDto(flaw));
        } else {
            result.setCode(404);
            result.setMsg("Flaw not found");
        }
        return result;
    }
    

    public AjaxResult<Void> addFlaw(AgvFlawDto dto) {
        AgvFlaw entity = toEntity(dto);
        int rows = flawMapper.insert(entity);
        AjaxResult<Void> res = new AjaxResult<>();
        res.setCode(rows > 0 ? 200 : 500);
        res.setMsg(rows > 0 ? "success" : "insert failed");
        return res;
    }

    public AjaxResult<Void> updateFlaw(AgvFlawDto dto) {
        AgvFlaw entity = toEntity(dto);
        int rows = flawMapper.updateById(entity);
        AjaxResult<Void> res = new AjaxResult<>();
        res.setCode(rows > 0 ? 200 : 500);
        res.setMsg(rows > 0 ? "success" : "update failed");
        return res;
    }

    public AjaxResult<Void> delFlaw(Long id) {
        int rows = flawMapper.deleteById(id);
        AjaxResult<Void> res = new AjaxResult<>();
        res.setCode(rows > 0 ? 200 : 404);
        res.setMsg(rows > 0 ? "success" : "delete failed");
        return res;
    }

    /**
     * 检查给定任务 ID 下是否所有缺陷都已确认
     */
    public AjaxResult<Void> checkAllConfirmed(Long taskId) {
        Long count = flawMapper.selectCount(new QueryWrapper<AgvFlaw>().eq("task_id", taskId).eq("confirmed", false));
        AjaxResult<Void> res = new AjaxResult<>();
        res.setCode(200);
        res.setMsg(count == 0 ? "all confirmed" : "pending confirm");
        return res;
    }

    public TableDataInfo<AgvFlawDto> listByTaskId(Long taskId) {
        List<AgvFlaw> list = flawMapper.selectList(new QueryWrapper<AgvFlaw>().eq("task_id", taskId));
        List<AgvFlawDto> rows = list.stream().map(this::toDto).collect(Collectors.toList());
        TableDataInfo<AgvFlawDto> info = new TableDataInfo<>();
        info.setTotal(rows.size());
        info.setRows(rows);
        info.setCode(200);
        info.setMsg("success");
        return info;
    }

    private AgvFlawDto toDto(AgvFlaw entity) {
        AgvFlawDto dto = new AgvFlawDto();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    private AgvFlaw toEntity(AgvFlawDto dto) {
        AgvFlaw entity = new AgvFlaw();
        BeanUtils.copyProperties(dto, entity);
        return entity;
    }
} 